// mandatory task 4/project object oriented programming

#include "Includes.hpp"

class RobustIO;

////////////////////////////// GLOBALS ///////////////////////////////
Players players;
Sports sports;
RobustIO robio;

//////////////////////////// PROGRAM ENTRYPOINT //////////////////////
int main() {
    bool running = true; // for checking if run loop is continuing
    char* command = NULL; // make and set to something not random memory

    readFromFile(); // read already saved from file

////////////////////////////// RUN LOOP //////////////////////////////
    do {
	printMenu(FLOOR); // print menu
	command = robio.readCommand("command"); // request and get 'command'
	switch(*command) {
	case 'N': // make new player/sport/division
	{
	    printMenu(MAKENEW); // print menu for register new
	    command = robio.readCommand("command"); // read command from user
	    switch(*command) { // act on user input
	    case 'P':
		players.addPlayer(); // add a player
		break;
	    case 'S':
		sports.addSport(); // add a sport
		break;
	    case 'D':
		sports.addDivision(); // add a division
		break;
	    default:
		printMenu(MAKENEW); // reprint menu
		break;
	    } // MAKENEW switch end
	} // case 'N'
	break;
	case 'P': // print all players or one by number or name
	{
	    printMenu(PRINTPLAYER); // print menu for print player
	    command = robio.readCommand("command"); // read command from user
	    if ((command[0] == 'A') && (strlen(command) < 2)) {
		players.printAllPlayers(); // display all players in list
	    } else if (isdigit(command[0])) {
		int tempnum = NULL;
		Itoa(tempnum, command, 10);
		players.printPlayer(tempnum);
	    } else {
		players.printPlayer(command);
	    } // if command[0] end
	} // case 'P' end
	break;
	case 'S': // print sport all or one by name to screen
	{
	    printMenu(PRINTSPORT); // print menu
	    command = robio.readCommand("command"); // read command from user
	    if (command[0] == 'A') {
		sports.printAllSports(); // display all sports in list
	    } else if (strlen(command) > 1) { // if strlen(command) is longer than one
		sports.printSport(command); // print one sport with 'command' name
	    } // if command[0] end
	} // case 'S' end
	break;
	case 'R': // remove player, sport or division
	{
	    printMenu(REMOVESID); // print menu for removal
	    command = robio.readCommand("command"); // read user input
	    switch(*command) { // act on user input
	    case 'P': // remove player
	    {
		char* tempname = robio.readName("player name or number"); // get name||number
		if (isdigit(tempname[0])) { // if read name is a number
		    int tempnum = NULL; // make and init. to something !random-mem.
		    Itoa(tempnum, tempname, 10); // make tempname into an int
		    players.deletePlayer(tempnum); // delete this player
		} else { // if tempname is not a number
		    players.deletePlayer(tempname, 0); // delete this player
		} // if isdigit end
	    }
	    break;
	    case 'S': // remove sport
	    {
		char* tempname = robio.readName("sport"); // get name of sport
		if (sports.deleteSport(tempname)) { // call deleteSport func.
		    cout << "\n" << tempname << " has been deleted"; // print success msg
		} else { // if the delete was a failure
		    cout << "\ncould not delete " << tempname; // print error
		} // if delete end
	    }
	    break;
	    case 'D': // remove division
	    {
		//char* tempname = robio.readName("sport containing division"); // get name 
		sports.deleteDiv(); // of sport // delete this sports division
	    }
	    break;
	    default: // default!
		printMenu(REMOVESID); // print menu for removal
		break;
	    } // switch command end
	} // case 'R' end
	break;
	case 'T':
		printWriteTable();
	    break;
	case 'F':
		{
			ifstream in("results.dta");
			if (in) {
				readResults(&in);
			} else {
				cout << "\n\tcould not open file 'result.dta'"; // print error
			}
			in.close();
		}
		break;
	case 'M':
		printWriteMatchResults();
		break;
	case 'C': // print/write schedule to screen/file
	    sports.printWriteSchedule();
	    break;
	case 'I': // print all players in a team to screen  
	{
	    displaytp();
	}
	break;
	case 'E': // edit players in a team
	    changepl();
	    break;
	case 'Q':
	    bool savequit;
	    savequit = robio.readInt("Do you want to save your work?", 1); // request and
	    if(savequit) { // if user wants to save                        // get 'savequit'
		writeToFile(&players); // write players to file
		writeToFile(&sports); // write sports to file
	    }
	    cout << "\n\tGoodbye\n"; // print 'goodbye' to be nice
	    running = false; // running no longer
	    break;
	default:
	    printMenu(FLOOR); //print menu again maybe this should print a LONG
	    // help message just to annoy the user?
	    break;
	}
    } while(running); // run loop end
    return 0;
} // end main()
